/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.dao;

import core.APersistenceManager;
import core.IPlayerDAO;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import persistence.*;
import validators.PlayerValidator;

/**
 *
 * @author Ivan
 */
public class PlayerDAO extends APersistenceManager<Player> implements IPlayerDAO {

    public PlayerDAO() {
        super(Player.class, new PlayerValidator());
    }

    @Override
    public Set<Player> getPlayersByGame(int idGame) {
        Set<Player> players = new HashSet<Player>();

        Game game = getEntityManager().find(Game.class, BigDecimal.valueOf(idGame));
        for (GameContract gc : game.getGameContractSet()) {
            Team t = gc.getTeamId();
            for (TeamContract tc : t.getTeamContractSet()) {
                players.add(tc.getPlayerId());
            }
        }

        return players;
    }

    @Override
    public Set<Player> getPlayersByOwner(int idUser) {
        SysUser sysUser = getEntityManager().find(SysUser.class, BigDecimal.valueOf(idUser));
        return sysUser.getPlayerSet();
    }

    @Override
    public Set<Player> getPlayersByTeam(int idTeam) {
        Set<Player> players = new HashSet<Player>();

        Team team = getEntityManager().find(Team.class, BigDecimal.valueOf(idTeam));
        for (TeamContract tc : team.getTeamContractSet()) {
            players.add(tc.getPlayerId());
        }
        return players;
    }

    @Override
    public void changeRating(int idPlayer, Rating r) {
        ///maybe it don't need. simple do it through the edit parent method
    }

    @Override
    public Rating getRatingOfPlayer(int idPlayer) {
        Player player = getEntityManager().find(Player.class, BigDecimal.valueOf(idPlayer));
        return player.getRatingId();
    }
}
